
package com.companytypemodule.dao;
import com.companytypemodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbCompanyTypeDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbCompanyType tbCompanyType=null;
    private List<TbCompanyType> tbCompanyTypeList=null;
    private Map<String,TbCompanyType> mapCompanyType=null;
    public boolean add(TbCompanyType inTbCompanyType){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_CompanyType(CompanyTypeName,CompanyTypeMemo,CreateTime) values(?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbCompanyType.getCompanyTypeName());
        pt.setString(2,inTbCompanyType.getCompanyTypeMemo());
        pt.setString(3,inTbCompanyType.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public boolean del(int inCompanyTypeId){
        return UtilDAO.del("Tb_CompanyType", "CompanyTypeId", inCompanyTypeId);
    }
    public boolean edit(TbCompanyType inTbCompanyType){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_CompanyType set CompanyTypeName=?,CompanyTypeMemo=?,CreateTime=? where CompanyTypeId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbCompanyType.getCompanyTypeName());
        pt.setString(2,inTbCompanyType.getCompanyTypeMemo());
        pt.setString(3,inTbCompanyType.getCreateTime());
        pt.setInt(4,inTbCompanyType.getCompanyTypeId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public TbCompanyType getTbCompanyTypeByCompanyTypeId(int inCompanyTypeId) {
        tbCompanyType=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyType where CompanyTypeId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inCompanyTypeId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbCompanyType == null)tbCompanyType = getTbCompanyType(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
          ConnectDB.closeSqlConnection();
            
        }
        return tbCompanyType;
    }
    public List<TbCompanyType> getTbCompanyTypeAll() {
        tbCompanyTypeList=new ArrayList<TbCompanyType>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyType";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyTypeList.add(getTbCompanyType(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbCompanyTypeList;
    }
    public List<TbCompanyType> getTbCompanyTypeListByCompanyTypeId(String inCompanyTypeId) {
        tbCompanyTypeList=new ArrayList<TbCompanyType>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyType where CompanyTypeId in("+inCompanyTypeId+") ";
            System.out.println("sql:"+sql);
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyTypeList.add(getTbCompanyType(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbCompanyTypeList;
    }
    /**
     * zhang
     *@author qingyu zhang
     *@function:
     * @return
     *2011-5-13
     *下午03:23:54
     *ProjectManager
     *com.companytypemodule.dao
     *Map<Integer,TbCompanyType>
     */
    public Map<String,TbCompanyType> getTbCompanyType(){
    	mapCompanyType= new HashMap<String,TbCompanyType>();
    	 try{
             conn = ConnectDB.getSqlServerConnection();
             sql = "select * from Tb_CompanyType";
             pt = conn.prepareStatement(sql);
             rs = pt.executeQuery();
             while (rs.next()) {
            	 TbCompanyType companyType =  getTbCompanyType(rs);
            	 System.out.println("公司名称"+companyType.getCompanyTypeName());
            	 mapCompanyType.put(String.valueOf(companyType.getCompanyTypeId()), companyType);
             }
        
         }catch(Exception e){
             e.printStackTrace();
         }finally {
        	 ConnectDB.closeSqlConnection();
         }
         return mapCompanyType;
    }
    public List<TbCompanyType> getTbCompanyTypeAll(VskPage vp) {
        tbCompanyTypeList=new ArrayList<TbCompanyType>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyType";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_CompanyType", "CompanyTypeId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyTypeList.add(getTbCompanyType(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbCompanyTypeList;
    }
    public TbCompanyType getTbCompanyType(ResultSet rs) throws Exception{
        TbCompanyType tbCompanyType = new TbCompanyType();
        tbCompanyType.setCompanyTypeId(rs.getInt("CompanyTypeId"));
        tbCompanyType.setCompanyTypeName(rs.getString("CompanyTypeName"));
        tbCompanyType.setCompanyTypeMemo(rs.getString("CompanyTypeMemo"));
        tbCompanyType.setCreateTime(rs.getString("CreateTime"));
        return tbCompanyType;
    }
}
